//
//  WSMCustomPageControl.m
//  WinesMall
//
//  Created by Isaac on 2018/4/14.
//  Copyright © 2018年 Isaac. All rights reserved.
//

#import "WSMCustomPageControl.h"
#define dotW 13 // 圆点宽
#define dotH 4  // 圆点高
#define magrin 8    // 圆点间距
@implementation WSMCustomPageControl


- (instancetype)init {
    self = [super init];
    if (self) {
        self.userInteractionEnabled = NO;
    }
    return self;
}
//-(void)setCurrentPage:(NSInteger)currentPage{
//    [super setCurrentPage:currentPage];
//    for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++) {
//    UIImageView* subview = [self.subviews objectAtIndex:subviewIndex];
//        // CGSize size; // size.height = 12; // // size.width = 12; //
////     [subview setFrame:CGRectMake(subview.frame.origin.x+10, subview.frame.origin.y,  15, 5)];
//    if (subviewIndex == currentPage) // subview.image =[UIImage imageNamed:@"60.png"];
//    {
//        subview.backgroundColor = [UIColor redColor];
//        subview.layer.cornerRadius = 0;
//        subview.layer.masksToBounds = YES;
//        
//    } else { //subview.image =[UIImage imageNamed:@"60.png"];
//        subview.backgroundColor = kWhiteColor;
//        subview.layer.cornerRadius = 0;
//        subview.layer.masksToBounds = YES;
//        
//    }
//    }
//    
//}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //计算圆点间距
    CGFloat marginX = dotW + magrin;
    //计算整个pageControll的宽度
    CGFloat newW = (self.subviews.count - 1 ) * marginX;
    
    //设置新frame
    self.frame = CGRectMake(SCREEN_WIDTH/2-(newW + dotW)/2, self.frame.origin.y, newW + dotW, self.frame.size.height);
    
    //遍历subview,设置圆点frame
    for (int i=0; i<[self.subviews count]; i++) {
        UIImageView* dot = [self.subviews objectAtIndex:i];
        
        if (i == self.currentPage) {
            dot.backgroundColor = [UIColor redColor];
            dot.layer.cornerRadius = 0;
            dot.layer.masksToBounds = YES;
            [dot setFrame:CGRectMake(i * marginX, dot.frame.origin.y, dotW, dotH)];
        }else {
            dot.backgroundColor = kWhiteColor;
            dot.layer.cornerRadius = 0;
            dot.layer.masksToBounds = YES;
            [dot setFrame:CGRectMake(i * marginX, dot.frame.origin.y, dotW, dotH)];
        }
    }
}
    
    

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
